package com.edu.web.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.edu.entity.PageBean;
import com.edu.entity.User;
import com.edu.service.UserBackgroundService;
import com.edu.service.impl.UserBackgroundServiceImpl;


/**
 * Servlet implementation class selectByUserServlet
 */
@WebServlet("/selectByUserServlet")
public class selectByUserServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public selectByUserServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("UTF-8");
		long userId = -1;
		long phone = -1;
		int state = -1;
		
		String result = request.getParameter("currentPageNum");
		String pageSizeS = request.getParameter("pageSize");
		
		int currentPageNum = Integer.parseInt(result==null?"1":result);
		int pageSize = Integer.parseInt(pageSizeS==null?"5":pageSizeS);//显示记录数
		
		String getId = request.getParameter("userId");
		String username = request.getParameter("userName");
		String userpass = request.getParameter("userPass");
		String getPhone = request.getParameter("phone");
		String getState = request.getParameter("state");
		String position = request.getParameter("position");
		request.setAttribute("username", username);
		request.setAttribute("userpass", userpass);
		request.setAttribute("getId", getId);
		request.setAttribute("getPhone", getPhone);
		request.setAttribute("getState", getState);
		request.setAttribute("position", position);
		System.out.println(getId);
		System.out.println(username);
		if(getId!=null && !"".equals(getId)){
			userId = Long.parseLong(getId);
		}
		if(getPhone!=null && !"".equals(getPhone)){
			phone = Long.parseLong(getPhone);
		}
		if(getState!=null && !"".equals(getState)){
			state = Integer.parseInt(getState);
		}
		User user = new User(userId,username,userpass,phone,position,state);
		UserBackgroundService userBackService = new UserBackgroundServiceImpl();
		PageBean<User> pageBean = userBackService.selectByUser(user,currentPageNum,pageSize);
		System.out.println(pageBean);
		request.setAttribute("pageBean",pageBean);
		//response.sendRedirect("selectByUser.jsp");
		request.getRequestDispatcher("selectByUser.jsp").forward(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
